<?php 
include("Connections/elevate954.php");
include_once('functions.php');

mysql_select_db($database_elevate954, $elevate954);

if(isset($_POST['pageId']) && !empty($_POST['pageId'])){
  $id=$_POST['pageId'];
}else{
  $id='0';
}

$pageLimit=PAGE_PER_NO*$id;                
$HTML='';$content = '';

if(isset($_POST['filterCat']) && !empty($_POST['filterCat']) && $_POST['filterCat'] !=0){
	$get_image = "select * from GALLERY where CATEGORY_ID=".$_POST['filterCat']." order by gallery_id desc limit $pageLimit,".PAGE_PER_NO;
}else{
	$get_image = "select * from GALLERY order by CATEGORY_ID,gallery_id desc limit $pageLimit,".PAGE_PER_NO;
}
$res_image = mysql_query($get_image);
$count_gallery = mysql_num_rows($res_image);
if($count_gallery > 0){
	$c=0;$lastCat=-1;
	
	if($_POST['filterCat'] >= 0){
		if($_POST['filterCat'] != 0)
			$imageCnt = "select * from GALLERY where CATEGORY_ID=".$_POST['filterCat'];
		else
			$imageCnt = "select * from GALLERY";
		$resCnt = mysql_query($imageCnt);
		$cntImg = mysql_num_rows($resCnt);

		$paginationCount=getPagination($cntImg);
			
		$content .='<ul class="tsc_pagination tsc_paginationC tsc_paginationC01">';
			/*<li class="first link" id="first">
			<a  href="javascript:void(0)" onclick="changePagination(\'0\',\'first\','.$_POST['filterCat'].')">F i r s t</a>
			</li>';*/
			for($i=0;$i<$paginationCount;$i++){
		 		if($i == 0){ $sel = 'current'; }else{ $sel = ''; }
				$content .='<li id="'.$i.'_no" class="link">
			<a class="'.$sel.' '.$i.'_no" href="javascript:void(0)" onclick="changePagination(\''.$i.'\',\''.$i.'_no\','.$_POST['filterCat'].')">
					  '.($i+1).'
				  </a>
			</li>';
			}
		 
			/*'<li class="last link" id="last">
	<a href="javascript:void(0)" onclick="changePagination(\''.($paginationCount-1).'\',\'last\','.$_POST['filterCat'].')">L a s t</a>
			</li>*/
			$content .='<li class="flash"></li>
		</ul>';
	}
	
	$HTML .= '<ul id="list" class="image-grid threecol portfolio-content">';
	
	while($row_image = mysql_fetch_array($res_image))
	{
		if($row_image['CATEGORY_ID'] != $lastCat){
			$get_gallery_cat = "select * from GALLERY_CAT where GALLERY_CAT_ID=".$row_image['CATEGORY_ID'];
			$res_gallery_cat = mysql_query($get_gallery_cat);
			$row_gallery_cat = mysql_fetch_array($res_gallery_cat);
			$c++;
		}
		$lastCat = $row_image['CATEGORY_ID'];
		
		$HTML .= '<li data-id="'.$c.'" data-type="'.str_replace(' ','_',$row_gallery_cat['CATEGORY_NAME']).'">';
		if($row_image['MEDIA_TYPE'] == "slider")
		{							
			$HTML .= '<div class="hover_slideshow">
						<a href="/image_uploads/'.$row_image['MEDIA_FILE'].'" rel="prettyPhoto[project1]" title="'.$row_image['IMAGE_NAME'].'"><img src="/image_uploads/'.$row_image['THUMBNAIL_IMAGE'].'" alt="" width="290" height="180"/></a>
                      </div>
                      <div class="text_box">
							<h4>'.$row_image['DESCRIPTION'].'</h4>
							<p>'.$row_image['IMAGE_NAME'].'</p>
                      </div>';
        }else if($row_image['MEDIA_TYPE'] == "video"){							
			$HTML .= '<div class="hover_video">
                     	<a href="'.$row_image['VIDEO_LINK'].'" rel="prettyPhoto" title="'.$row_image['IMAGE_NAME'].'">
                        <img src="/image_uploads/'.$row_image['MEDIA_FILE'].'" alt="" width="290" height="180"/></a>
                      </div>
                      <div class="text_box">
                       	<h4>'.$row_image['DESCRIPTION'].'</h4>
                       	<p>'.$row_image['IMAGE_NAME'].'</p>
                      </div>';
        }else if($row_image['MEDIA_TYPE'] == "image"){							
			$HTML .= '<div class="hover_image">
                        <a href="/image_uploads/'.$row_image['MEDIA_FILE'].'" rel="prettyPhoto" title="A single image">
                        <img src="/image_uploads/'.$row_image['THUMBNAIL_IMAGE'].'" alt="" width="290" height="160"/></a>
                      </div> 
                      <div class="text_box">
						<h4>'.$row_image['DESCRIPTION'].'</h4>
						<p>'.$row_image['IMAGE_NAME'].'</p>
                      </div>';
        }
        $HTML .= '</li>';
	}
	$HTML .= '</ul>';
}else{
	$HTML = 'No Data Found';
}

echo json_encode(array($HTML,$content));
